/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 5/18/2008
 * Time: 10:18 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text.RegularExpressions;

namespace popLib
{
	 public class ValidationLib{
	 	public static string errMessage = "";
		public static int errCount = 0;
	
		public static void ResetValidate() 
		{
			errMessage = "";
			errCount = 0;
		}
	
		public static bool IsSameString(string objName1,string objName2, string str1,string str2) 
		{
			if(str1.Equals(str2)) return true;
			if(objName1 != null && objName2 != null)
			{
				errCount++;
				errMessage+="\n"+errCount + ". " + objName1+" must be same as "+objName2;
			}
			return false;		
		}
	
		public static bool IsSelected(string objName, int index, int startindex) 
		{
			//string nochar=@"\n\r\t ";
			if(index>=startindex) return true;
			if(objName != null)
			{
				errCount++;
				errMessage+="\n"+errCount + ". " + objName+" must be selected one item.";
			}
			return false;
		}
	
		public static bool IsSelected(string objName, int index) 
		{
			return IsSelected(objName,index,0);
		}
	
		public static bool IsNotBlank(string objName, string str) 
		{
			//string nochar=@"\n\r\t ";
			if(str!= null && str != "") return true;
			if(objName != null)
			{
				errCount++;
				errMessage+="\n"+errCount + ". " + objName+" must not be blank.";
			}
			return false;
		}
	
		public static bool IsBoolean(string objName, string boolean)
		{
			if(boolean.Equals("1")||boolean.Equals("0")||boolean.Equals("True")||boolean.Equals("False"))
				return true;
			if(objName != null)
			{
				errCount++;
				errMessage+="\n"+errCount + ". " + objName+" must be logical.";
			}
			return false;		
		}
	
		public static bool IsNumeric(string objName, string n, bool isFloatNumber)
		{
	        string numeric = "1234567890.";
			if(!isFloatNumber) numeric = "1234567890";
			for(int i=0;i<n.Length;i++){
				if(numeric.IndexOf(n.Substring(i,1)) == -1 )
				{
					if(objName != null)
					{
						errCount++;
						errMessage+="\n" + errCount + ". " + objName+" must be a number.";
					}
					return false;
				}
			}
			if( n.IndexOf(".") == 0 || ( n.IndexOf(".") > 0 && n.IndexOf(".") != n.LastIndexOf(".") ))
			{			
				if(objName != null)
				{
					errCount++;
					errMessage+="\n" + errCount + ". " + objName+" must be a number.";
				}
				return false;
			}
			return true;
		}
	
		
		public static bool IsNumberInRange(string objName,string n,int min,int max,bool isFloatNumber) 
		{
			if(IsNumeric(objName, n, isFloatNumber) && Convert.ToInt32(n) >= min &&  Convert.ToInt32(n) <= max)
				return true;
			if(objName != null)
			{
				errCount++;
				errMessage+="\n" + errCount + ". " + objName+" must be a number(in range "+min+" to "+max+").";
			}
			return false;
			
		}
	
		public static bool IsMatch(string sValue,string sRegEx)
		{
			Regex regex = new Regex(sRegEx);
			Match  match = regex.Match(sValue);
			return match.Success;
		}
	
		public static bool IsPhone(string objName,string phone)
		{
			return IsPhone(objName, phone, true);
		}
	
		public static bool IsPhone(string objName,string phone, bool allowBlank) 
		{
			if(allowBlank == true) if(phone==null || phone=="") return true;
			if(IsNotBlank(objName,phone) == false) return false;
			bool retval=true;
			Regex phoneNumberPattern = new Regex("^(\\d{2,3})-(\\d{6,7})$"); //(?:\\d{3}\\p{Punct}?\\s?){1,2}\\d{4}");
			
			Match  match = phoneNumberPattern.Match(phone);
			if(!match.Success) 
			{
				if(objName != null)
				{
					errCount++;
					errMessage+="\n"+errCount + ". " + objName+" must follow the format (Ex: 65-6859698).";
				}
				retval= false;
			}
			return retval;
		}
	
		public static bool IsMobile(string objName,string phone, bool allowBlank) 
		{
			if(allowBlank == true) if(phone==null || phone=="") return true;
			if(IsNotBlank(objName,phone) == false) return false;
			bool retval=true;
			Regex phoneNumberPattern = new Regex("^(\\d{4})-(\\d{6})$"); //(?:\\d{3}\\p{Punct}?\\s?){1,2}\\d{4}");
			
			Match  match = phoneNumberPattern.Match(phone);
			if(!match.Success) 
			{
				if(objName != null)
				{
					errCount++;
					errMessage+="\n"+errCount + ". " + objName+" must follow the mobile format number (Ex: 0918-154785).";
				}
				retval= false;
			}
			return retval;
		}
	
		public static bool IsEmail(string objName,string email)
		{
			if(email==null || email=="") return true;
			bool retval=false;
			Regex EmailPattern =new Regex("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
			Match match= EmailPattern.Match(email);
			if(!match.Success) 
			{
				if(objName != null)
				{
					errCount++;
					errMessage+="\n"+errCount + ". " + objName+" is not valid.";
				}
				retval=false;
			}
			return retval;
		}
	
		public static bool IsCorrectBirthday(string objName,DateTime birthday,int fromYear, int toYear)
		{
			if(fromYear<= birthday.Year && birthday.Year <=toYear) return true;
			if(objName != null)
			{
				errCount++;
				errMessage+="\n"+errCount + ". " + objName+" is not valid.";
			}
			return false;
		}
	
		public static bool IsCorrectTime(string objName,string str)
		{
				//check is it number
			if( IsNumeric(null,str,true) && 
					//have . or not          have only one .							 . in the end of line .12 or .1													  the number ater . smaller than 60
				( ( str.IndexOf(".") > 0 && str.IndexOf(".") == str.LastIndexOf(".") && ( str.IndexOf(".") == (str.Length - 3) || str.IndexOf(".") == (str.Length - 2) ) && Convert.ToInt16(str.Substring(str.IndexOf(".")+1)) < 60 ) 
				   || ( str.IndexOf(".") == -1) ) )
				return true;			
			if(objName != null)
			{
				errCount++;
				errMessage+="\n"+errCount + ". " + objName+" is not valid(hhh.mm).";
			}
			return false;
		}
	 }
}
